# 二叉树建树以及遍历
class node():
    def __init__(self,data,lchild=None,rchild=None):
        self.data=data
        self.lchild=lchild
        self.rchild=rchild

class binary_tree():
    def __init__(self):
        self.root = None
        self.tree_queue = []
    def level_build(self,node):
        self.tree_queue.append(node)
        if self.root is None:
            self.root=node
        else:
            if self.tree_queue[0].lchild is None:
                self.tree_queue[0].lchild=node
            else:
                self.tree_queue[0].rchild=node
                self.tree_queue.pop(0)

    def pre_order(self,current_node):
        if current_node:
            print(current_node.data,end=' ')
            self.pre_order(current_node.lchild)
            self.pre_order(current_node.rchild)

    def mid_order(self,current_node):
        if current_node:
            self.mid_order(current_node.lchild)
            print(current_node.data, end=' ')
            self.mid_order(current_node.rchild)

    def post_order(self,current_node):
        if current_node:
            self.post_order(current_node.lchild)
            self.post_order(current_node.rchild)
            print(current_node.data, end=' ')

    def level_order(self):
        tree_queue = []
        tree_queue.append(self.root)
        while tree_queue:
            current_node = tree_queue.pop(0)
            print(current_node.data, end=' ')
            if current_node.lchild:
                tree_queue.append(current_node.lchild)
            if current_node.rchild:
                tree_queue.append(current_node.rchild)


if __name__ == '__main__':
    tree = binary_tree()
    for i in range(1,11):
        new_node = node(i)
        tree.level_build(new_node)
    print("前序遍历结果如下：")
    tree.pre_order(tree.root)
    print("\n中序遍历结果如下：")
    tree.mid_order(tree.root)
    print("\n后序遍历结果如下：")
    tree.post_order(tree.root)
    print("\n层序遍历结果如下：")
    tree.level_order()